package com.activemq.demo.activemq_service.controller;

import com.activemq.demo.activemq_service.service.MessageProcessingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/health")
public class HealthController {

    @Autowired
    private MessageProcessingService messageProcessingService;

    @GetMapping
    public ResponseEntity<Map<String, Object>> healthStatus() {
        Map<String, Object> healthInfo = new HashMap<>();
        healthInfo.put("status", "UP");
        healthInfo.put("timestamp", System.currentTimeMillis());

        // 添加消息处理统计
        Map<String, Object> messageStats = new HashMap<>();
        messageStats.put("processed", messageProcessingService.getProcessedCount());
        messageStats.put("failed", messageProcessingService.getFailedCount());
        messageStats.put("deadLetter", messageProcessingService.getDeadLetterCount());

        healthInfo.put("messageStats", messageStats);

        return ResponseEntity.ok(healthInfo);
    }

    @GetMapping("/metrics")
    public ResponseEntity<Map<String, Object>> metrics() {
        Map<String, Object> metrics = new HashMap<>();

        metrics.put("processedMessages", messageProcessingService.getProcessedCount());
        metrics.put("failedMessages", messageProcessingService.getFailedCount());
        metrics.put("deadLetterMessages", messageProcessingService.getDeadLetterCount());

        return ResponseEntity.ok(metrics);
    }
}